//图片懒加载方法
export const lazyload = (imgs) => {
    // //获取当前窗口的可视高度
    console.log('触发了图片懒加载哦~~~');
    let seeHeight = window.innerHeight;
    imgs.forEach((elem, index) => {
        //如果图片盒子距离窗口顶部的高度小于可视窗口，证明该盒子进入了用户视野
        //则开始加载真正的图片
        if (elem.getBoundingClientRect().top < seeHeight) {
            elem.setAttribute("src", elem.getAttribute("data-img"));
        }
    });
}
//防抖函数
// debounce 函数接受一个函数和延迟执行的时间作为参数
export const debounce = (func, wait) => {
    let timer;
    return function () {
        let context = this; // 注意 this 指向
        let args = arguments; // arguments中存着e
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(this, args);
        }, wait);
    };
}

// 格式化时间毫秒转分秒
export const formatTime = (time) => {
    // 取整
    time = ~~time;
    var formatTime;
    if (time < 10) {
        formatTime = "00:0" + time;
    } else if (time < 60) {
        formatTime = "00:" + time;
    } else {
        var m = ~~parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
        if (m < 10) {
            m = "0" + m;
        }
        var s = ~~parseInt((time % (1000 * 60)) / 1000);
        if (s < 10) {
            s = "0" + s;
        }
        formatTime = m + ":" + s;
    }
    return formatTime;
}

//换算播放次数
export const huansuan = (val) => {
    let num = parseInt(val);
    let xx = num / 10000;
    if (xx >= 1) {
        if (xx / 10000 >= 1) {
            xx = parseInt(xx / 10000) + "亿";
        } else {
            xx = parseInt(xx) + "万";
        }
        return xx;
    } else {
        return num;
    }
}
// 日期格式化
export const dateFormat = (str, type) => {
    let date = new Date(str)
    let year = date.getFullYear()
    let month = formatZero(date.getMonth() + 1, 2)
    let day = formatZero(date.getDate(), 2)
    let hour = formatZero(date.getHours(), 2)
    let minute = formatZero(date.getMinutes(), 2)
    let seconds = formatZero(date.getSeconds(), 2)
    if (type == 'YYYY-MM-DD') {
        return `${year}-${month}-${day}`
    } else if (type == 'YYYY-MM-DD HH:MM:SS') {
        return `${year}-${month}-${day} ${hour}:${minute}:${seconds}`
    } else if (type == 'MM/DD  HH:MM:SS') {
        return `${month}/${day} ${hour}:${minute}:${seconds}`
    }
}
export const formatZero = (num, len) => {
    if (String(num).length > len) return num
    return (Array(len).join(0) + num).slice(-len)
}

//秒数转换为分钟
export const timeMinutes = (time) => {
    if (time / 10 < 1) {
        return time = `00:0${time}`;
    } else if (time / 10 < 6) {
        return time = `00:${parseInt(time / 10)}${time % 10}`;
    } else {
        return time = `0${parseInt(time / 60)}:${parseInt((time - parseInt(time / 60) * 60) / 10)}${time % 10}`;
    }
}

//解析歌词
export const parsingLyric = (val) => {
    let lyric = val.split('\n');
    let wodeci = [];
    for (let i = 0; i < lyric.length; i++) {
        let s = lyric[i].indexOf('[');
        let e = lyric[i].indexOf(']');
        //把时间换算成s
        let time = lyric[i].substring(s + 1, e);
        let m = time.indexOf(':');
        let min = parseInt(time.substring(0, m));
        let miao = time.indexOf('.');
        let seconed = parseInt(time.substring(m + 1, miao));
        let haomiao = (time.substr(miao + 1));
        seconed = (seconed + '.' + haomiao) - 0;
        var zongji = min * 60 + seconed;
        let ci = lyric[i].substring(e + 1,);
        wodeci.push({ time: zongji, lineLyric: ci, });
    }
    wodeci.forEach((elem, index) => {
        if (index < wodeci.length - 1) {
            if (index == wodeci.length - 2) {
                elem.endtime = 9999;
            } else {
                elem.endtime = wodeci[index + 1].time;
            }
        } else {
            elem.endtime = 9999;
        }
    });
    console.log('我的词', wodeci);
    return wodeci;
}